package cn.qdu.service;

import cn.qdu.dao.FriendDao;
import cn.qdu.dao.UserDao;
import cn.qdu.model.FriendRequest;
import cn.qdu.model.User;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class FriendService {
    private FriendDao friendDao = new FriendDao();
    private UserDao userDao = new UserDao();

    public boolean sendFriendRequest(String userId, String friendId) {
        try {
            // 检查是否已经是好友
            if (friendDao.isFriend(userId, friendId)) {
                return false;
            }
            // 检查是否已有待处理的请求
            if (friendDao.hasPendingRequest(userId, friendId)) {
                return false;
            }
            return friendDao.sendFriendRequest(userId, friendId);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean acceptFriendRequest(String userId, String friendId) {
        try {
            return friendDao.acceptFriendRequest(userId, friendId);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<FriendRequest> getFriendRequests(String userId) {
        try {
            return friendDao.getFriendRequests(userId);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<User> getFriends(String userId) {
        try {
            return friendDao.getFriends(userId);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<User> searchUsers(String keyword, String currentUserId) {
        try {
            List<User> searchResults = userDao.searchUsers(keyword, currentUserId);

            // 为每个用户设置好友状态
            if (searchResults != null) {
                for (User user : searchResults) {
                    boolean isFriend = friendDao.isFriend(currentUserId, user.getUserId());
                    boolean hasPending = friendDao.hasPendingRequest(currentUserId, user.getUserId());

                    // 使用包装类
                    user.setIsFriend(isFriend);
                    user.setHasPendingRequest(hasPending);

                    // 调试输出
                    System.out.println("用户: " + user.getNickname() +
                            ", isFriend: " + user.getIsFriend() +
                            ", hasPendingRequest: " + user.getHasPendingRequest());
                }
            }

            return searchResults;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<User> getRecommendedFriends(String userId) {
        try {
            List<User> recommended = friendDao.getRecommendedFriends(userId);

            // 为推荐好友设置共同好友数
            if (recommended != null) {
                for (User user : recommended) {
                    int mutual = friendDao.getMutualFriendsCount(userId, user.getUserId());
                    user.setMutualFriends(mutual);
                }
            }

            return recommended;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 添加共同好友数查询方法
    public int getMutualFriendsCount(String userId1, String userId2) {
        try {
            return friendDao.getMutualFriendsCount(userId1, userId2);
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public boolean rejectFriendRequest(String userId, String friendId) {
        try {
            return friendDao.rejectFriendRequest(userId, friendId);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean removeFriend(String userId, String friendId) {
        try {
            return friendDao.removeFriend(userId, friendId);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 添加用户年龄计算方法
    public int calculateAge(Date birthDate) {
        if (birthDate == null) return 0;
        Calendar dob = Calendar.getInstance();
        dob.setTime(birthDate);
        Calendar today = Calendar.getInstance();
        int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
        if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
            age--;
        }
        return age;
    }
}